//
//  MovieDetailView.m
//  豆瓣终极版
//
//  Created by dllo on 15/12/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "MovieDetailView.h"
#import "UIImageView+WebCache.h"
#import "MovieDetail.h"
@interface MovieDetailView ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *posterImageView;
@property (nonatomic, retain) UILabel *ratingLab; //评分
@property (nonatomic, retain) UILabel *releaseDateLab;// 上映日期
@property (nonatomic, retain) UILabel *runtimeLab; // 电影时长
@property (nonatomic, retain) UILabel *genresLab; // 电影类型
@property (nonatomic, retain) UILabel *countryLab; // 国家
@property (nonatomic, retain) UILabel *actorsLab; // 制作人
@property (nonatomic, retain) UILabel *actorsContentLab;
@property (nonatomic, retain) UILabel *plotLab; // 电影情节
@property (nonatomic, retain) UILabel *plotContentLab;



@end
@implementation MovieDetailView

- (void)dealloc{
    [_scrollView release];
    [_posterImageView release];
    [_ratingLab release];
    [_releaseDateLab release];
    [_runtimeLab release];
    [_genresLab release];
    [_countryLab release];
    [_actorsLab release];
    [_actorsContentLab release];
    [_plotLab release];
    [_plotContentLab release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(375, 800);
    [self addSubview:self.scrollView];
    [_scrollView release];
    
    self.posterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 130, 180)];
    self.posterImageView.image = [UIImage imageNamed:@""];
    //    self.posterImageView.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:self.posterImageView];
    [_scrollView release];
    
    self.ratingLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 10, 200, 40)];
    self.ratingLab.text = @"";
    [self.scrollView addSubview:self.ratingLab];
    [_ratingLab release];
    
    self.releaseDateLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 50, 200, 40)];
    self.releaseDateLab.text = @"2014";
    [self.scrollView addSubview:self.releaseDateLab];
    [_releaseDateLab release];
    
    self.runtimeLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 90, 200, 40)];
    self.runtimeLab.text = @"126 min";
    [self.scrollView addSubview:self.runtimeLab];
    [_runtimeLab release];
    
    self.genresLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 130, 200, 40)];
    self.genresLab.text = @"动作";
    [self.scrollView addSubview:self.genresLab];
    [_genresLab release];
    
    self.countryLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 170, 200, 40)];
    self.countryLab.text = @"美国";
    [self.scrollView addSubview:self.countryLab];
    [_countryLab release];
    
    self.actorsLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 200, 40)];
    self.actorsLab.text = @"制作人";
    self.actorsLab.font = [UIFont systemFontOfSize:24];
    [self.scrollView addSubview:self.actorsLab];
    [_actorsLab release];
    
    self.actorsContentLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 330, 80)];
    //    self.actorsContentLab.backgroundColor = [UIColor lightGrayColor];
    self.actorsContentLab.numberOfLines = 0;
    self.actorsContentLab.text = @"";
    [self.scrollView addSubview:self.actorsContentLab];
    [_actorsContentLab release];
    
    self.plotLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 340, 200, 40)];
    self.plotLab.text = @"电影情节";
    self.plotLab.font = [UIFont systemFontOfSize:24];
    [self.scrollView addSubview:self.plotLab];
    [_plotLab release];
    
    self.plotContentLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 400, 330, 290)];
    //    self.plotContentLab.backgroundColor = [UIColor lightGrayColor];
    self.plotContentLab.numberOfLines = 0;
    self.plotContentLab.text = @"";
    [self.scrollView addSubview:self.plotContentLab];
    [_plotContentLab release];

    
}

- (void)setMoviedetail:(MovieDetail *)moviedetail{
    if (_moviedetail != moviedetail) {
        [_moviedetail release];
        _moviedetail = [moviedetail retain];
    }
    
    // 放到主线程里
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURL *url = [NSURL URLWithString:_moviedetail.poster];
        [self.posterImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder.png"]];
        
        NSString *str = @"评分";
        self.ratingLab.text = [NSString stringWithFormat:@"%@: %@    (%@ 评论)", str, _moviedetail.rating, _moviedetail.rating_count];
        
        self.releaseDateLab.text = _moviedetail.release_date;
        self.runtimeLab.text = _moviedetail.runtime;
        self.countryLab.text = _moviedetail.country;
        self.actorsContentLab.text = _moviedetail.actors;
        self.plotContentLab.text = _moviedetail.plot_simple;
        [self.plotContentLab sizeToFit];
        self.scrollView.contentSize = CGSizeMake(375, self.plotContentLab.frame.size.height + 500);
        
    });
    

    
    
    
}
@end
